<?php
/**
 * Created by PhpStorm.
 * User: worry
 * Date: 2016/12/15
 * Time: 23:23
 */

namespace Common\Model;


use Common\Lib\ChuanglanSmsApi;
use Think\Model;

class SmsLogsModel extends Model
{

    private  $config = [
        'times'=>3,//限制发送次数
        'expire'=>3600,//限制发送次数时效
        'interval'=>60,//限制发送时间间隔（秒）
    ];


    /**
     * 配置
     * @param array $config
     */
    public function setConfig($config = [])
    {
        $this->config = array_merge($this->config, (array)$config);
    }
    /**
     * 发送短信
     * @param string $phone 手机号码
     * @return array
     */
    public function send($phone, $func = '', $user_id='')
    {
        //检查手机号
        if(!$phone) {
            return ['status'=>false, 'info'=>'手机号不能为空'];
        }
        //检查限制次数
        if($this->config['times'] && $this->config['expire'] && $this->where(['sms_phone'=>$phone, 'sms_state'=>0, 'sms_create_time'=>['gt', time() - $this->config['expire']]])->count() >= $this->config['times']) {
            return ['status'=>false, 'info'=>"该手机号发送次数已超过{$this->config['times']}次"];
        }
        //检查限制间隔
        if($this->config['interval'] && ($time = $this->where(['sms_phone'=>$phone, 'sms_state'=>0])->order('sms_create_time DESC')->getField('sms_create_time')) && $time > time() - $this->config['interval']) {
            return ['status'=>false, 'info'=>($this->config['interval'] + $time - time())."秒后才可重新发送"];
        }
        $code = rand(100000,999999);
        $CL = new ChuanglanSmsApi(['account'=>'N9972738', 'password'=>'4Alrs7XBYc3d7b']);
        $response = $CL->sendSMS($phone, "验证码：{$code}");
        $result = $CL->execResult($response);

        //是否发送成功
        if($result[1] == 0){
            //发送成功写入数据库信息
            $log['sms_return_code']=json_encode($result);
            $log['sms_phone']=(string)$phone;
            $log['sms_content']='';
            $log['sms_code']=(string)$code;
            $log['sms_user_id']=(string)$user_id;
            $log['sms_function']=$func;
            $log['sms_create_time']=time();
            $this->add($log);
            return ['status'=>true, 'info'=>'发送成功'];
        }
        return ['status'=>false, 'info'=>'发送失败', 'error'=>$result[1]];
    }

    /**
     * 验证验证码
     * @param $mobile
     * @param $code
     * @param int $expire 自定义有效期
     * @return int
     */
    public function verify($mobile, $code, $expire = 0)
    {
        $sms_info = $this->where(array('sms_phone'=>$mobile))->order('sms_create_time DESC')->find();

        if(!$sms_info){
            return 1;
        }
        if($expire && time() - $sms_info['sms_create_time'] > $expire){
            return 2;
        }
        if($sms_info['sms_state'] == 1){
            return 4;
        }
        if($code == $sms_info['sms_code']){
            if(!C('API_DEBUG')){
                $sms_info['sms_state'] = 1;
                //旧的验证码全部失效
                $this->where(['sms_phone'=>$mobile])->save(['sms_state'=>1]);
            }
            return 0;
        } else {
            return 3;
        }
    }
}